'''
dir = /mnt/nas/datasets/diction
关键词分类 
    coat 
    leather 
    sweater 
    paike 
    trench 
    leafur 
    slipdress 
    dress 
    jean 
日期关键词分类
    0808
    0811 0812 0813 0814 0815
    0818 0819 0820 0821 0822
    0825 0826 0827 0828 0829
    0901 0902 0903 0904 0905
    0908 0909 0910 0911 0912
    0915 0916 0917 0918 0919
    0922 0923 0924 0925 0926
    0928 0929 0930
处理结果关键词分类
    ends with _img_clo
    ends with _img_clo_diff
    ends with _img_clo_diff_furry
    ends with _img_clo_diff_pattern
    ends with _img_front_back

统计输出
每个分类 对应 每个处理结果 的 日期列表
比如 输出 
    coat-[_img_clo]:[0808,0812,.....]
    coat-[_img_clo_diff]:[0808,0812,.....]
    ...
    leather-[_img_clo]:[0808,0812,.....]
    ...
'''
# INSERT_YOUR_CODE

import os
from collections import defaultdict

dir_path = '/mnt/nas/datasets/diction'

# 1. 定义大类和处理结果后缀
categories = [
    'coat', 'leather', 'sweater', 
    'paike', 'trench', 'leafur',
    'slipdress', 'dress', 'feather',
    'jean',
    'suitset','suit','trousers',
    'jacket','widepants','hoodie',
    'formalatt','weddress',
]
process_suffixes = [
    '_img_clo',
    '_img_clo_diff',
    '_img_clo_diff_furry',
    '_img_clo_diff_pattern',
    '_img_front_back',
]
# 日期字符串2位月+2位日
date_keywords = [
    '0808','0811','0812','0813','0814','0815',
    '0818','0819','0820','0821','0822',
    '0825','0826','0827','0828','0829',
    '0901','0902','0903','0904','0905',
    '0908','0909','0910','0911','0912',
    '0915','0916','0917','0918','0919',
    '0922','0923','0924','0925','0926',
    '0928','0929','0930',
    '1009','1010','1011',
    '1013','1014','1016',
    '1020','1021','1022','1023','1024',
    '1027','1028','1029','1030','1031',
    '1103','1104','1105','1106','1107',
    '1110','1111',
]

# 2. 扫描目录
all_dirs = os.listdir(dir_path)
# 只保留目录（不是文件且不以.开头）
all_dirs = [d for d in all_dirs if not d.startswith('.') and os.path.isdir(os.path.join(dir_path, d))]

# 3. 统计结构：category -> suffix -> list of dates
stat_result = defaultdict(lambda: defaultdict(list))
# 4. 分类统计（优化版本）
for d in all_dirs:
    matched_cat = next((cat for cat in categories if d.startswith(cat)), None)
    if not matched_cat:
        continue
    remain = d[len(matched_cat):]
    if len(remain) < 4:
        continue
    maybe_date = remain[:4]
    remain2 = remain[4:]
    if maybe_date not in date_keywords:
        continue
    matched_suffix = next((suf for suf in process_suffixes if remain2 == suf), None)
    if not matched_suffix:
        continue
    # 统计
    stat_result[matched_cat][matched_suffix].append(maybe_date)



    # 去掉 restore 的逻辑,简化代码功能,只是检测某个文件夹是否存在
    # names txt的生成交给其他功能完成
    # # 检查 names.txt
    # abs_dir = os.path.join(dir_path, d)
    # names_txt = os.path.join(abs_dir, 'names.txt')
    # if not os.path.exists(names_txt) or maybe_date > '1025':
    #     ret = os.system(f"bash restore.sh {abs_dir}")
    #     if ret != 0:
    #         print(f"Failed to restore for {abs_dir}, return code: {ret}")

    # with open(names_txt) as f:
    #     # 读取names.txt统计以.jpg结尾的行数
    #     count_jpg = 0
    #     for line in f:
    #         if line.strip().endswith('.jpg'):
    #             count_jpg += 1
    #     # print(f"{d}: {count_jpg} jpg files")
    
    # stat_result[matched_cat][matched_suffix].append(maybe_date)
    

# 5. 打印结果
for cat in categories:
    for suf in process_suffixes:
        dates_list = stat_result[cat][suf]
        if dates_list:
            print(f"{cat}-[{suf}]: \n\t{sorted(dates_list)}")
    print()

